﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManage
{
    public partial class FormUserAdd : Form
    {
        public string upriority;
        public FormUserAdd()
        {
            InitializeComponent();
        }

        private void FormUserAdd_Load(object sender, EventArgs e)
        {
            if (this.upriority[0] < '1')
                this.comboBox1.Items.Add('1');
            if (this.upriority[0] < '2')
                this.comboBox1.Items.Add('2');
            if(this.upriority[0]<'3')
                this.comboBox1.Items.Add("3");
            this.comboBox1.Items.Add("4");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string uname = this.textBoxUserName.Text.ToString();
            string upasswd = this.textBoxPasswd.Text.ToString();
            string uprior = this.comboBox1.SelectedItem.ToString();

            DBConnect dbConn = new DBConnect();
            // 判断用户名是否存在
            string qstr = "SELECT COUNT(uid) FROM users_info WHERE uname = '" + uname + "'";
            int cnt = dbConn.Count(qstr);
            if (cnt != 0)
            {
                MessageBox.Show("用户 “" + uname + "”已存在", "提示");
                return;
            }

            // Insert
            qstr = "INSERT INTO users_info (uname,upasswd,upriority) VALUES ('" + uname + "','" + upasswd + "','" + uprior + "')";
            dbConn.Insert(qstr);

            // 判断是否插入成功
            qstr = "SELECT COUNT(uid) FROM users_info WHERE uname = '" + uname;
            cnt = dbConn.Count(qstr);
            if (cnt == 1)
            {
                MessageBox.Show("成功添加用户" + uname, "提示");
            }
            else
            {
                MessageBox.Show("添加用户" + uname + "失败！", "提示");
            }
        }
    }
}
